/**
 * Created by Administrator on 15-11-30.
 */
;(function(){
	var Defaults = {
		offsetX : 0,
		offsetY : 0,
		onShow : function(){},
		onHide : function(){}
	};
	function Tooltip(){
		var that = this;
		this.$win = $(window);
		this.screenWidth = 0;
		this.screenHeight = 0;
		this.downBox = $("#ui-tooltip-wrap");
		this.onHide = Defaults.onHide;
		this.downBox.on("mouseover",function(e){
			that.show();
		})
		this.downBox.on("mouseleave",function(e){
			that.hide();
		})
	}
	Tooltip.prototype = {
		tip : function(trigger,content,opt){
			if(!trigger) return false;
			var horDir = "left";
			var verDir = "top";
			var downBox = this.downBox;
			var downBoxCon = downBox.children(".ui-tooltip-con");
			var opt = $.extend({},Defaults,opt||{});
			var content = typeof content=="function" ? content() : (content || "");
			var offset = trigger.offset();
			var left = offset.left+Number(opt.offsetX);
			var top = offset.top+trigger.height()+Number(opt.offsetY);
			downBoxCon.html(content);
			if(!this.screenWidth) this.screenWidth = this.getScreenWidth();
			if(!this.screenHeight) this.screenHeight = this.getScreenHeight();
			var boxWidth = downBox.width();
			var boxHeight = downBox.height();

//			if(left+boxWidth>this.screenWidth){
//				var dis = (left+boxWidth)-this.screenWidth;
//				left = left-(boxWidth-dis);
//				horDir = "right";
//			}
//			if(top+boxHeight>this.screenHeight){
//				top = offset.top-boxHeight;
//				verDir = "bottom";
//			}

			downBox.css({
				display : "block",
				left : left,
				top : top,
				zIndex : 10001
			}).addClass(horDir).addClass(verDir);

			opt.onShow();

		},
		hide : function(){
			this.downBox.hide().css({zIndex:-1});
			this.onHide();
		},
		show : function(){
			this.downBox.show().css({zIndex:10001});
		},
		getScreenWidth : function(){
			return this.$win.width();
		},
		getScreenHeight : function(){
			return this.$win.height();
		}
	}

	window["PFT"] = window["PFT"] || {};
	PFT["UI"] = PFT.UI || {};
	PFT["UI"]["Tooltip"] = Tooltip;

})();